﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Apollo.Web;
using Apollo.Core;
using Apollo;
using Apollo.Modules.Commissioning;

namespace Apollo.Website
{
    public partial class Controls_Edit_FormItemCommissionApplicability : System.Web.UI.UserControl, IFormItem
    {
        public FormItem Item { get; set; }

        public object TargetObject { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.FormItemRelationRole.TargetObject = this.TargetObject;
            this.FormItemRelationRole.Item = new FormItem()
            {
                Col = 0,
                Row = 0,
                Group = this.Item.Group,
                Projection = new Projection("Role", this.Item.Group.Form.Entity.MappedClass),
                Type = FormItemType.Normal
            };

            this.FormItemRelationPosition.TargetObject = this.TargetObject;
            this.FormItemRelationPosition.Item = new FormItem()
            {
                Col = 0,
                Row = 0,
                Group = this.Item.Group,
                Projection = new Projection("Position", this.Item.Group.Form.Entity.MappedClass),
                Type = FormItemType.Normal
            };

            this.FormItemRelationEmployee.TargetObject = this.TargetObject;
            this.FormItemRelationEmployee.Item = new FormItem()
            {
                Col = 0,
                Row = 0,
                Group = this.Item.Group,
                Projection = new Projection("Employee", this.Item.Group.Form.Entity.MappedClass),
                Type = FormItemType.Normal
            };

            this.FormItemRelationContract.TargetObject = this.TargetObject;
            this.FormItemRelationContract.Item = new FormItem()
            {
                Col = 0,
                Row = 0,
                Group = this.Item.Group,
                Projection = new Projection("Contract", this.Item.Group.Form.Entity.MappedClass),
                Type = FormItemType.Normal
            };

            DropDownListCommissionApplicability.Attributes.Add("onchange", "ChangeCommissionApplicability(this)");

            if (this.TargetObject != null)
            {
                string selectedApplicability = ((int)(this.TargetObject as ICommissionApplicability).CommissionApplicability).ToString();

                foreach (ListItem li in DropDownListCommissionApplicability.Items)
                    li.Selected = (li.Value == selectedApplicability);


            }
        }


        public ActionMessage Save(object obj)
        {
            ICommissionApplicability ca = obj as ICommissionApplicability;

            if (ca == null)
            {
                return new ActionMessage("Commission can not be null!", ActionMessageType.Error);
            }

            switch (DropDownListCommissionApplicability.SelectedValue)
            {
                case "-1": ca.CommissionApplicability = CommissionApplicability.Accelerator;
                    FormItemRelationRole.Save(obj);
                    FormItemRelationPosition.Save(obj);
                    FormItemRelationEmployee.Save(obj);
                    FormItemRelationContract.Save(obj);

                    return null;
                case "0": ca.CommissionApplicability = CommissionApplicability.All;
                    ca.Role = null;
                    ca.Position = null;
                    ca.Employee = null;
                    ca.Contract = null;
                    break;
                case "1": ca.CommissionApplicability = CommissionApplicability.Role;
                    ca.Position = null;
                    ca.Employee = null;
                    ca.Contract = null;
                    FormItemRelationRole.Item.Type = FormItemType.Required;
                    return FormItemRelationRole.Save(obj);
                case "2": ca.CommissionApplicability = CommissionApplicability.Position;
                    ca.Role = null;
                    ca.Employee = null;
                    ca.Contract = null;
                    FormItemRelationPosition.Item.Type = FormItemType.Required;
                    return FormItemRelationPosition.Save(obj);
                case "3": ca.CommissionApplicability = CommissionApplicability.Employee;
                    ca.Role = null;
                    ca.Position = null;
                    ca.Contract = null;
                    FormItemRelationEmployee.Item.Type = FormItemType.Required;
                    return FormItemRelationEmployee.Save(obj);
                case "4": ca.CommissionApplicability = CommissionApplicability.Contract;
                    ca.Role = null;
                    ca.Position = null;
                    ca.Employee = null;
                    FormItemRelationContract.Item.Type = FormItemType.Required;
                    return FormItemRelationContract.Save(obj);
            }

            return null;
        }
    }
}